# -*- encoding=utf8 -*-
__author__ = "jwjwchen"
"""主文件，执行该文件即可"""

import sys,os
sys.path.append(os.getcwd())
from bring_up_fish_config import *
from bring_up_fish_fun import *
from airtest.core.api import *
import random
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
x,y = poco.get_screen_size() #获取屏幕分辨率



#--------------初始化咸鱼app-----------------------    
package = "com.taobao.idlefish"
init_idle_fish(package)
#-------------------------------------------------
while True:
    try:
        #------------------随机浏览首页宝贝并且点赞，询价------------------------
        view_home_time = random.randint(view_home_time_min,view_home_time_max)
        print ("准备浏览首页宝贝 %s 次"%view_home_time)
        wait_for_appearance_and_click(poco(nameMatches='^闲鱼.*状态$',selected=False))
        for _ in range(view_home_time):
            swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5) 
            sleep(random.randint(1,5))
            swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
            sleep(random.randint(1,5))
            swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
            sleep(5)
            try:
                wait_for_appearance_and_click(poco(name="com.taobao.idlefish:id/content_image")[2])
                sleep(random.randint(1,5))
                swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
                sleep(random.randint(1,5))
                swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
            except:
                wait_for_appearance_and_click(poco(name="com.taobao.idlefish:id/content_image")[1])
                sleep(random.randint(1,5))
                swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
                sleep(random.randint(1,5))
                swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
            sleep(5)
            if poco(text="我想要",touchable=True).exists():
                poco(text="我想要",touchable=True).click()
                if i_want_switch:
                    wait_for_appearance_and_click(poco(text="想跟TA说点什么呢",touchable=True))
                    text(auto_choice_rand_conment("enquiry"))
                    sleep(random.randint(1,3))
                    wait_for_appearance_and_click(poco(text="发送",touchable=True)) #发送
                    keyevent("BACK")
                sleep(random.randint(1,3))
                keyevent("BACK")
                wait_for_appearance_and_click(poco(text="超赞"))
                sleep(random.randint(1,3))
                keyevent("BACK")
                sleep(random.randint(1,3))
            else:
                keyevent("BACK")
                sleep(random.randint(1,3))
                
        #------------------随机浏览鱼塘点赞，评论------------------------
        start_app(package)
        view_fishpood_time = random.randint(view_fishpood_time_min,view_fishpood_time_max)
        print ("准备浏览鱼塘 %s 次"%view_fishpood_time)
        execu = True
        for _ in range(view_fishpood_time):
            if execu == True:
                wait_for_appearance_and_click(poco(nameMatches='^鱼塘.*$',selected=False))
                wait_for_appearance_and_click(poco(name='推荐',desc='推荐'))
                swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5) 
                sleep(random.randint(2,5))
                swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5) 
            #获取全部帖子列表
            posting_list = poco(type='android.support.v7.widget.RecyclerView',name='android.support.v7.widget.RecyclerView').children()
            count = len(posting_list)
#             print (count)
            for i in range(count):
                execu = False
#                 print (posting_list[i])
                posting_list[i].click()
                sleep(5)
                if poco(desc="卖闲置",touchable=True).exists():
                    continue
                if poco(name="com.taobao.idlefish:id/lt_cmt_like").exists():
                    sleep(random.randint(1,10))
                    swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
                    swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
                    sleep(random.randint(1,10))
                    swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
                    sleep(random.randint(1,10))
                    swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
                    sleep(random.randint(1,10))
                    wait_for_appearance_and_click(poco(name="com.taobao.idlefish:id/lt_cmt_like")) #点赞
                    if comment_switch:
                        #-------------评论功能-------------
                        wait_for_appearance_and_click(poco(name="com.taobao.idlefish:id/ft_cmt_detail_replay")) #点击评论
                        text(choice_comment())
                        wait_for_appearance_and_click(poco(name="com.taobao.idlefish:id/tv_comment_send")) #发送
                        #--------------------------
                    keyevent("BACK")
                elif poco(text="超赞").exists():
                    poco(text="超赞").click()
                    keyevent("BACK")
                else:
                    keyevent("BACK")
                sleep(random.randint(1,5))
            swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
            sleep(random.randint(1,3))
            swipe((0.5*x,0.8*y),(0.5*x,0.2*y),duration=0.5)
            sleep(random.randint(1,3))
            
    except Exception as e:
        print ("出现异常:",e)
        init_idle_fish(package)
            

    
    









        


